package com.zl.smartmall.library.po;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Property;
import net.tsz.afinal.annotation.sqlite.Table;
import net.tsz.afinal.annotation.sqlite.Transient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.util.Constants;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 广告类(全局各个页面的展示广告)
 * @author 陈希然
 *
 */
@Table(name = "tb_AdvertInfo")
public class AdvertInfo implements Parcelable {

	@Id
	private int id;
	@Property
	private int width;
	@Property
	private int height;
	@Property
	private int advertType;
	@Property
	private int affiliation;//属于什么位置的广告
	@Property
	private String title;
	@Property
	private String picture;
	@Property
	private String valueMapJson;
	@Transient
	private Map<String, Object> valueMap = new HashMap<String, Object>();
	private String weburl;

	public AdvertInfo(){
		
	}

	public String getWeburl() {
		return weburl;
	}

	public void setWeburl(String weburl) {
		this.weburl = weburl;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getAdvertType() {
		return advertType;
	}

	public void setAdvertType(int advertType) {
		this.advertType = advertType;
	}

	public int getAffiliation() {
		return affiliation;
	}

	public void setAffiliation(int affiliation) {
		this.affiliation = affiliation;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}  

	public String getValueMapJson() {
		return valueMapJson;
	}

	public void setValueMapJson(String valueMapJson) {
		this.valueMapJson = valueMapJson;
	}

	public Map<String, Object> getValueMap() {
		return valueMap;
	}

	public void setValueMap(Map<String, Object> valueMap) {
		this.valueMap = valueMap;
	}
	
	public void decode() throws JSONException {
		if(valueMapJson != null && !"".equals(valueMapJson)) {
			JSONObject jsonObject = new JSONObject(valueMapJson);
			switch (advertType) {
			case Constants.ADVERT_TYPE_BRANDS_LIST:
				valueMap.put("brand_id", jsonObject.get("brand_id"));
				valueMap.put("brand_name", jsonObject.get("brand_name"));
				break;
	
			case Constants.ADVERT_TYPE_PRODUCT_DETAIL:
				valueMap.put("product_id", jsonObject.get("product_id"));
				valueMap.put("shop_id", jsonObject.get("shop_id"));
				break;
	
			}
		}
	}
	
	public void encode() throws JSONException {
		JSONObject jsonObject = new JSONObject();
		switch (advertType) {
		case Constants.ADVERT_TYPE_BRANDS_LIST:
			jsonObject.put("brand_id", (Integer) valueMap.get("brand_id"));
			jsonObject.put("brand_name", (String) valueMap.get("brand_name"));
			break;

		case Constants.ADVERT_TYPE_PRODUCT_DETAIL:
			jsonObject.put("product_id", (Integer) valueMap.get("product_id"));
			jsonObject.put("shop_id", (Integer) valueMap.get("shop_id"));
			break;
		}
		
		valueMapJson = jsonObject.toString();
	}

	private static AdvertInfo parse(JSONObject jsonObject, int affiliation) throws JSONException{
		if(jsonObject == null){
			return null;
		}
		
		AdvertInfo data = new AdvertInfo();
		data.advertType = jsonObject.getInt("ad_type");
		data.height = jsonObject.getInt("ad_pic_height");
		data.width = jsonObject.getInt("ad_pic_width");
		data.picture = jsonObject.getString("ad_pic");
		data.title = jsonObject.getString("ad_name");
		try {
			data.weburl = jsonObject.getString("weburl");
		} catch (Exception e) {
			data.weburl = "";
		}
		
		data.affiliation = affiliation;
		
		switch (data.advertType) {
		case Constants.ADVERT_TYPE_BRANDS_LIST:
			data.valueMap.put("brand_id", jsonObject.getJSONObject("linkurl").getInt("brand_id"));
			data.valueMap.put("brand_name", jsonObject.getJSONObject("linkurl").getString("brand_name"));
			break;

		case Constants.ADVERT_TYPE_PRODUCT_DETAIL:
			data.valueMap.put("shop_id", jsonObject.getJSONObject("linkurl").getInt("shop_id"));
			data.valueMap.put("product_id", jsonObject.getJSONObject("linkurl").getInt("product_id"));
			break;
		}
		
		return data;
	}
	
    public static ArrayList<AdvertInfo> parseArray(JSONArray jsonArray, int affiliation) throws JSONException{
		ArrayList<AdvertInfo> datas = new ArrayList<AdvertInfo>();
		
		if(jsonArray == null || jsonArray.length() == 0){
			return datas;
		}
		
		for(int i = 0; i < jsonArray.length(); i++){
			AdvertInfo data = parse(jsonArray.getJSONObject(i), affiliation);
			if(data != null){
				datas.add(data);
			}
		}
		return datas;
		
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(width);
		dest.writeInt(height);
		dest.writeInt(advertType);
		dest.writeInt(affiliation);
		dest.writeString(title);
		dest.writeString(picture);
		dest.writeString(valueMapJson);
		dest.writeString(weburl);
	}
	public AdvertInfo(Parcel source){
		width = source.readInt();
		height = source.readInt();
		advertType = source.readInt();
		affiliation = source.readInt();
		title = source.readString();
		picture = source.readString();
		valueMapJson = source.readString();
		weburl = source.readString();
	}
	
	public static final Parcelable.Creator<AdvertInfo> CREATOR = new Creator<AdvertInfo>() {
		
		@Override
		public AdvertInfo[] newArray(int size) {
			return new AdvertInfo[size];
		}
		
		@Override
		public AdvertInfo createFromParcel(Parcel source) {
			return new AdvertInfo(source);
		}
	};
	
}
